package demo.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Scanner;

public class TopicProducer {

    public static void produce() throws JMSException {
        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = factory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic("popular-news");
        MessageProducer producer = session.createProducer(topic);
        TextMessage message = null;
        Scanner scanner = new Scanner(System.in);
        while(true) {
            System.out.println("please input msg here(input 'exit' to exit):");
            String msg = scanner.nextLine();
            if("exit".equals(msg)) break;
            message = session.createTextMessage(msg);
            //8.发送消息
            producer.send(message);
        }
        producer.close();
        session.close();
        connection.close();
    }

    public static void main(String[] args) throws JMSException {
        produce();
    }
}
